package com.speed.util.ecxel;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelResolver {

	public static List<TempData> importExcelData(InputStream input, boolean isSkip, List<ImportTempData> temps)
			throws IOException {
		Workbook wb = new HSSFWorkbook(input);
		Sheet sheet0 = wb.getSheetAt(0);
		return resolveRowData(sheet0, isSkip, temps);
	}

	/**
	 * 
	 * @param sheet
	 * @param isSkip
	 *            是否跳过第一行
	 */
	private static List<TempData> resolveRowData(Sheet sheet, boolean isSkip, List<ImportTempData> temps) {
		int lastRowNum = sheet.getLastRowNum();
		int firstRowNum = isSkip ? 1 : 0;
		List<TempData> datas = new ArrayList<TempData>();
		for (; firstRowNum <= lastRowNum; firstRowNum++) {
			Row row = sheet.getRow(firstRowNum);
			TempData tempData = new TempData();
			for (ImportTempData td : temps) {
				try {
					td.inputData(row, tempData);
				} catch (Exception e) {
					System.out.println("类型错误，将跳过输入此格");
					e.printStackTrace();
				}
			}
			datas.add(tempData);
		}
		return datas;
	}

}
